package com.chengh.nami.core.security.service;

import com.chengh.nami.core.async.AsyncManager;
import com.chengh.nami.core.async.factory.AsyncFactory;
import com.chengh.nami.core.security.contextHolder.AuthenticationContextHolder;
import com.chengh.nami.common.constants.Constants;
import com.chengh.nami.common.domain.pojo.SysUser;
import com.chengh.nami.common.utils.SecurityUtils;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

/**
 * 密码服务
 *
 * @author chengh
 */
@Component
public class PasswordService {

    public void validate(SysUser user) {
        Authentication usernamePasswordAuthenticationToken = AuthenticationContextHolder.getContext();
        String password = usernamePasswordAuthenticationToken.getCredentials().toString();
        boolean b = SecurityUtils.matchesPassword(password, user.getPassword());
        if (!b) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(user.getUsername(), Constants.LOGIN_FAIL,"密码错误"));
            throw new BadCredentialsException("密码不正确");
        }
    }
}
